package com.zw.yzk.kabu.presenter;

import android.support.v7.app.AppCompatActivity;

import com.zw.yzk.kabu.R;
import com.zw.yzk.kabu.base.BaseSubscriber;
import com.zw.yzk.kabu.data.network.entity.CollectedGoodsEntity;
import com.zw.yzk.kabu.data.network.task.GetCollectedGoodsTask;
import com.zw.yzk.kabu.domain.repository.UseCase;
import com.zw.yzk.kabu.management.LoadingDialogManager;
import com.zw.yzk.kabu.mapper.CollectedGoodsMapper;
import com.zw.yzk.kabu.view.view.CollectedGoodsView;

import javax.inject.Inject;
import javax.inject.Named;

/**
 * Created by zhanwei on 2017/3/12.
 */

public class CollectedGoodsPresenter implements Presenter {

    private UseCase getCollectedList;
    private AppCompatActivity activity;
    private CollectedGoodsMapper mapper;
    private CollectedGoodsView collectedGoodsView;

    @Inject
    public CollectedGoodsPresenter(@Named("collectedGoodsUseCase") UseCase getCollectedList,
                                   CollectedGoodsMapper mapper,
                                   AppCompatActivity activity) {
        this.getCollectedList = getCollectedList;
        this.activity = activity;
        this.mapper = mapper;
    }

    public void setView(CollectedGoodsView view) {
        this.collectedGoodsView = view;
    }

    public void getCollectedList(int start) {
        getCollectedList.execute(new CollectedGoodsSubscriber(activity), new GetCollectedGoodsTask(start));
    }


    @Override
    public void resume() {

    }

    @Override
    public void pause() {

    }

    @Override
    public void destroy() {
        getCollectedList.unSubscribe();
    }

    private class CollectedGoodsSubscriber extends BaseSubscriber<CollectedGoodsEntity> {

        public CollectedGoodsSubscriber(AppCompatActivity appCompatActivity) {
            super(appCompatActivity);
        }

        @Override
        public String addErrorMessage() {
            return activity.getResources().getString(R.string.get_collected_goods_failed);
        }

        @Override
        public void onNext(CollectedGoodsEntity collectedGoodsEntity) {
            super.onNext(collectedGoodsEntity);
            collectedGoodsView.getCollectedGoodsSuccess(mapper.transfer(collectedGoodsEntity));
        }

        @Override
        public void onCompleted() {
            super.onCompleted();
            LoadingDialogManager.getInstance().dismissLoadingDialog();
        }

        @Override
        public void onError(Throwable e) {
            super.onError(e);
            collectedGoodsView.getCollectedGoodsFailed();
        }
    }
}
